home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / StringUtil.c < prev    next >
Text File  |  1996-04-11  |  4KB  |  168 lines

  1. /*****
  2.  *
  3.  *    StringUtil.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include <string.h>
  16.  
  17. #include "compiler_stuff.h"
  18.  
  19. #include "StringUtil.h"
  20.  
  21.  
  22. /***  FUNCTIONS  ***/
  23.  
  24. /* Copy pascal format srcStr to the destStr */
  25. void
  26. StringPascalCopy ( char *srcStr, char *destStr )
  27. {
  28.     BlockMove ( srcStr, destStr, (srcStr[nil]) + 1 );
  29. } /* StringPascalCopy */
  30.  
  31.  
  32. /* count the occurances of theChar in theString */
  33. long
  34. StringCountChar ( char *theString, char theChar )
  35. {
  36.     long    total;
  37.     char *    tempStr;
  38.     
  39.     total    = nil;
  40.     tempStr    = theString;
  41.     
  42.     do
  43.     {
  44.         /* get a ptr to the next instance of theChar */
  45.         tempStr = StringChar ( tempStr, theChar );
  46.         
  47.         if ( (tempStr != NULL) && (*tempStr != nil) )
  48.         {
  49.             tempStr++;
  50.             total++;
  51.         }
  52.     } while ( (tempStr != NULL) && (*tempStr != nil) );
  53.     
  54.     return total;
  55. } /* StringCountChar */
  56.  
  57.  
  58. /* Returns true if any chars were converted */
  59. Boolean
  60. StringConvertCharToChar ( char *ioString, char fromChar, char toChar )
  61. {
  62.     Boolean    didConvert;
  63.     
  64.     didConvert = false;
  65.     
  66.     while ( ioString[0] != nil )
  67.     {
  68.         if ( ioString[0] == fromChar )
  69.         {
  70.             ioString[0] = toChar;
  71.             didConvert  = true;
  72.         }
  73.         ioString++;
  74.     }
  75.     
  76.     return didConvert;
  77. } /* StringConvertCharToChar */
  78.  
  79.  
  80. /* working version of strchr for TPM */
  81. #if kCompiling_For_Symantec
  82. char *
  83. StringChar ( const char *theString, unsigned char theChar )
  84. {
  85.     do
  86.     {
  87.         if ( *theString == theChar )
  88.         {
  89.             /* found the character, return a pointer to it */
  90.             return (char *)theString;
  91.         }
  92.         theString++;
  93.     } while ( *theString != nil );
  94.     
  95.     /* didn't find the character */
  96.     return NULL;
  97. } /* StringChar */
  98. #endif
  99.  
  100.  
  101. /**  TEXT DRAWING FUNCTIONS  **/
  102.  
  103. /* Font, size, style, etc. must be set before this function is called. */
  104. void
  105. StringDrawInRect ( StringPtr theString, Rect theRect )
  106. {
  107.     if ( theString[nil] > nil )
  108.     {
  109.         StringDrawTextInRect ( (char *)(theString + 1), theString[nil], theRect );
  110.     }
  111. } /* StringDrawInRect */
  112.  
  113.  
  114. /* returns the number of characters not drawn because of insufficient space.
  115.     zero if every character drawn */
  116. long
  117. StringDrawTextInRect ( char *theText, long totalLength, Rect theRect )
  118. {
  119.     char *        currentStartOfText;
  120.     long        charsLeftInText;
  121.     long        currentNumCharsToPrint;
  122.     FontInfo    fInfo;
  123.     short        textLineHeight;
  124.     short        currentVPosition;
  125.     short        hPosition;
  126.     StyledLineBreakCode        theBreakCode;
  127.     Fixed        rectWidth;
  128.     Fixed        textWidth;
  129.     
  130.     /* initialize text values */
  131.     currentStartOfText        = theText;
  132.     charsLeftInText            = totalLength;
  133.     currentNumCharsToPrint    = charsLeftInText;
  134.     
  135.     GetFontInfo ( &fInfo );
  136.     
  137.     /* initialize spacing */
  138.     textLineHeight        = fInfo.ascent + fInfo.descent + fInfo.leading;
  139.     currentVPosition    = theRect.top + fInfo.ascent;
  140.     hPosition            = theRect.left;
  141.     rectWidth            = Long2Fix ( theRect.right - theRect.left );
  142.     
  143.     /* clear the rectangle where the text is to be displayed */
  144.     EraseRect ( &theRect );
  145.     
  146.     /* while there are still characters in the text that haven't been drawn
  147.         and there is still room in the Rect for drawing more characters */
  148.     while ( (charsLeftInText > nil) && (currentVPosition < theRect.bottom) )
  149.     {
  150.         textWidth = rectWidth;
  151.         
  152.         theBreakCode = StyledLineBreak ( currentStartOfText, charsLeftInText, nil,
  153.             charsLeftInText, nil, &textWidth, ¤tNumCharsToPrint);
  154.         
  155.         MoveTo   ( hPosition, currentVPosition );
  156.         DrawText ( currentStartOfText, nil, currentNumCharsToPrint );
  157.         
  158.         currentVPosition   += textLineHeight;
  159.         charsLeftInText    -= currentNumCharsToPrint;
  160.         currentStartOfText += currentNumCharsToPrint;
  161.     } /* end of while */
  162.     
  163.     return charsLeftInText;
  164. } /* StringDrawTextInRect */
  165.  
  166.  
  167. /***  EOF  ***/
  168.